package com.example.interviewdemo.sync;

/**
 * @program my-project-model
 * @description: synchronized 关键字锁定方法和代码块的方法
 * /usr/local/software/work/idea/my-project-model/interview-demo/src/main/java/com/example/interviewdemo/sync
 * 1.这个目录下执行javac命令
 * javac Main.java
 * 2.javap -v
 * 3.javap -v Main.class
 * @author: lee
 * @create: 2023/01/05 20:30
 */

/**
 * Classfile /usr/local/software/work/idea/my-project-model/interview-demo/src/main/java/com/example/interviewdemo/sync/Main.class
 *   Last modified 2023年1月5日; size 432 bytes
 *   SHA-256 checksum f9af18aa72c876702f191bc5023e13f2ca16b077914acc80039bc23db5125135
 *   Compiled from "Main.java"
 * public class com.example.interviewdemo.sync.Main
 *   minor version: 0
 *   major version: 61
 *   flags: (0x0021) ACC_PUBLIC, ACC_SUPER
 *   this_class: #21                         // com/example/interviewdemo/sync/Main
 *   super_class: #2                         // java/lang/Object
 *   interfaces: 0, fields: 0, methods: 2, attributes: 1
 * Constant pool:
 *    #1 = Methodref          #2.#3          // java/lang/Object."<init>":()V
 *    #2 = Class              #4             // java/lang/Object
 *    #3 = NameAndType        #5:#6          // "<init>":()V
 *    #4 = Utf8               java/lang/Object
 *    #5 = Utf8               <init>
 *    #6 = Utf8               ()V
 *    #7 = Fieldref           #8.#9          // java/lang/System.out:Ljava/io/PrintStream;
 *    #8 = Class              #10            // java/lang/System
 *    #9 = NameAndType        #11:#12        // out:Ljava/io/PrintStream;
 *   #10 = Utf8               java/lang/System
 *   #11 = Utf8               out
 *   #12 = Utf8               Ljava/io/PrintStream;
 *   #13 = String             #14            // synchronized同步方法
 *   #14 = Utf8               synchronized同步方法
 *   #15 = Methodref          #16.#17        // java/io/PrintStream.println:(Ljava/lang/String;)V
 *   #16 = Class              #18            // java/io/PrintStream
 *   #17 = NameAndType        #19:#20        // println:(Ljava/lang/String;)V
 *   #18 = Utf8               java/io/PrintStream
 *   #19 = Utf8               println
 *   #20 = Utf8               (Ljava/lang/String;)V
 *   #21 = Class              #22            // com/example/interviewdemo/sync/Main
 *   #22 = Utf8               com/example/interviewdemo/sync/Main
 *   #23 = Utf8               Code
 *   #24 = Utf8               LineNumberTable
 *   #25 = Utf8               test
 *   #26 = Utf8               SourceFile
 *   #27 = Utf8               Main.java
 * {
 *   public com.example.interviewdemo.sync.Main();
 *     descriptor: ()V
 *     flags: (0x0001) ACC_PUBLIC
 *     Code:
 *       stack=1, locals=1, args_size=1
 *          0: aload_0
 *          1: invokespecial #1                  // Method java/lang/Object."<init>":()V
 *          4: return
 *       LineNumberTable:
 *         line 12: 0
 *
 *   public synchronized void test();
 *     descriptor: ()V
 *     flags: (0x0021) ACC_PUBLIC, ACC_SYNCHRONIZED
 *     Code:
 *       stack=2, locals=1, args_size=1
 *          0: getstatic     #7                  // Field java/lang/System.out:Ljava/io/PrintStream;
 *          3: ldc           #13                 // String synchronized同步方法
 *          5: invokevirtual #15                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
 *          8: return
 *       LineNumberTable:
 *         line 14: 0
 *         line 15: 8
 * }
 * SourceFile: "Main.java"
 */
public class Main {
    /*同步方法*/
//    public synchronized void test() {
//        System.out.println("synchronized同步方法");
//    }
    /*同步代码块*/
    private String tag = "tag";

    public void test() {
        synchronized (tag) {
            System.out.println("同步代码块");
        }
    }
}
